<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" :size="size" v-if="mode === 'list'">
                <el-row>
                    <el-col :span=6>
                        <el-form-item label="公众号" prop="mpId">
                            <el-select v-model="condition.search.mpId" placeholder="请选择公众号" style="width:200px;">
                                <el-option
                                    v-for="item in mpInfos"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                                    {{ item.name }}
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="素材标题" prop="title">
                            <el-input v-model="condition.search.title"
                                      placeholder="素材标题"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="素材类型" prop="type">
                            <el-select v-model="condition.search.type"
                                       placeholder="素材类型">
                                <el-option label="文本"
                                           value="TEXT"></el-option>
                                <el-option label="图片"
                                           value="IMAGE"></el-option>
                                <el-option label="视频"
                                           value="VIDEO"></el-option>
                                <el-option label="卡券"
                                           value="CARD"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" @click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="success" @click="showAddDialog">新增</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            :data="tableData"
            :size="size"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="60">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="type"
                :formatter="formatType"
                width="100px"
                label="素材类型">
            </el-table-column>
            <el-table-column
                prop="appName"
                width="100px"
                label="公众号">
            </el-table-column>
            <el-table-column
                prop="title"
                width="150px"
                label="素材标题">
            </el-table-column>
            <el-table-column
                prop="content"
                width="200px"
                label="内容">
                <template v-slot="scope">
                    <mp-material-show :info="scope.row" :preview-src-list="fileUrls"></mp-material-show>
                </template>
            </el-table-column>
            <el-table-column
                v-if="mode === 'list'"
                prop="description"
                label="素材描述">
            </el-table-column>
            <el-table-column
                v-if="mode === 'list'"
                prop="mediaId"
                label="微信媒体ID">
            </el-table-column>
            <el-table-column
                v-if="mode === 'list'"
                prop="mediaExpiryTime"
                label="微信媒体过期时间">
            </el-table-column>
            <el-table-column
                v-if="mode === 'list'"
                prop="createTime"
                label="创建时间">
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                width="80px">
                <template v-slot="scope">
                    <div>
                        <el-button @click="onDelete(scope.row)" type="text" size="small" v-if="mode === 'list'">删除
                        </el-button>
                        <el-button @click="onSelect(scope.row)" type="text" size="small" v-else>选择
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-dialog :title=addOrUpdateDialogTitle width="30%" :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="120px" :size="size">
                <el-form-item label="选择公众号" v-if="!this.mpId">
                    <el-select v-model="addOrUpdateForm.mpId" placeholder="选择公众号" style="width:200px;">
                        <el-option
                            v-for="item in mpInfos"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            {{ item.name }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="素材标题">
                    <el-input v-model="addOrUpdateForm.title"
                              placeholder="素材标题"></el-input>
                </el-form-item>
                <el-form-item label="素材类型">
                    <el-select v-model="addOrUpdateForm.type"
                               placeholder="素材类型">
                        <el-option label="文本"
                                   value="TEXT"></el-option>
                        <el-option label="图片"
                                   value="IMAGE"></el-option>
                        <el-option label="视频"
                                   value="VIDEO"></el-option>
                        <el-option label="卡券"
                                   value="CARD"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="文本内容" v-if="addOrUpdateForm.type == 'TEXT'">
                    <el-input type="textarea" v-model="addOrUpdateForm.content" :autosize="{minRows: 2}"
                              placeholder="请输入文本内容"></el-input>
                </el-form-item>
                <el-form-item label="上传资源"
                              v-if="addOrUpdateForm.type == 'IMAGE' || addOrUpdateForm.type == 'VIDEO'">
                    <el-button @click="selectAlbumDialogVisible = true" size="mini" type="success">
                        选择资源
                    </el-button>
                    <div v-if="selectedAlbum.id" style="margin-top: 10px">
                        <media-show :url="selectedAlbum.url"></media-show>
                    </div>
                </el-form-item>
                <el-form-item label="素材描述">
                    <el-input v-model="addOrUpdateForm.description" type="textarea" :autosize="{minRows: 2}"
                              placeholder="素材描述"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addOrUpdateDialogVisible = false" :size="size">取 消</el-button>
                <el-button @click="addOrUpdateConfirm" type="primary" :size="size">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="选择资源" width="70%" :visible.sync="selectAlbumDialogVisible" append-to-body>
            <album ref="albumSelect" size="small" @select="onSelectAlbum" mode="select"></album>
            <div slot="footer" class="dialog-footer"></div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addMpMaterial,
    deleteMpMaterial,
    getMpInfoByPage,
    getMpMaterialByPage,
    getUploadUrl,
    updateMpMaterial
} from '@/api/api'
import TablePopover from "@/component/single/TablePopover";
import MpMaterialShow from "@/component/single/MpMaterialShow";
import MediaShow from "@/component/single/MediaShow";
import Album from "@/component/Album";

export default {
    name: "mp-material",
    components: {TablePopover, MpMaterialShow, MediaShow, Album},
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {},
            // 文件url数组
            fileUrls: [],

            // 公众号列表
            mpInfos: [],

            selectAlbumDialogVisible: false,
            selectedAlbum: [],
        }
    },
    // 这里定义需要父组件传过来的东西
    props: {
        mpId: String,
        size: {
            type: String,
            default: 'small'
        },
        // 默认列表模式
        mode: {
            type: String,
            default: 'list'
        },
    },
    methods: {
        upload() {
            return getUploadUrl();
        },
        onSelectAlbum(row) {
            this.selectedAlbum = row;
            this.selectAlbumDialogVisible = false;
            this.addOrUpdateForm.fileUrl = row.url;
            this.addOrUpdateForm.fileSize = row.size;
            this.addOrUpdateForm.fileLocation = row.fileLocation;

            this.$set(this.addOrUpdateForm, "title", row.title);
            this.$set(this.addOrUpdateForm, "description", row.description);
        },
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        formatType: function (row, column, cellValue) {
            if (cellValue == 'TEXT') {
                return "文本";
            } else if (cellValue == 'IMAGE') {
                return "图片";
            } else if (cellValue == 'VIDEO') {
                return "视频";
            } else if (cellValue == 'CARD') {
                return "卡券";
            } else {
                return "未知";
            }
        },
        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
            if (this.mpId) {
                this.addOrUpdateForm.mpId = this.mpId;
            }
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addMpMaterial(this.addOrUpdateForm).then(data => {
                if (data.respCode == 1000) {
                    this.addOrUpdateDialogVisible = false;
                    this.getData();
                }
            });
        },
        update: function () {
            updateMpMaterial(this.addOrUpdateForm).then(data => {
                if (data.respCode == 1000) {
                    this.addOrUpdateDialogVisible = false;
                    this.getData();
                }
            });
        },
        onSelect: function (row) {
            this.$emit('select', row)
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteMpMaterial({id: row.id}).then(data => {
                    if (data.respCode == 1000) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getData();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取公众号列表
        getMpInfos: function () {
            let condition = {
                page: 1,
                rows: 100,
                search: {
                    accountType: 'OFFICIAL_ACCOUNT'
                }
            };
            getMpInfoByPage(condition).then(data => {
                this.mpInfos = data.body.rows;
            });
        },
        // 获取表格数据
        getData: function () {
            if (this.mpId) {
                this.condition.search.mpId = this.mpId;
            }
            getMpMaterialByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;
                this.tableData.forEach((item, index, arr) => {
                    if (item.type == 'IMAGE') {
                        this.fileUrls.push(item.fileUrl);
                    }
                })
            });
            this.getMpInfos();
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
